import { readFileSync } from 'fs'
import chalk from 'chalk'

const msgPath = process.env.HUSKY_GIT_PARAMS
const msg = readFileSync(msgPath, 'utf-8').trim()

const commitRE =
  /^(revert: )?(feat|fix|docs|style|refactor|perf|test|init|chore|release)(\(.+\))?: .{1,50}/

if (!commitRE.test(msg)) {
  console.log()
  console.error(
    `  ${chalk.bgRed.white(' ERROR ')} ${chalk.red(
      `提交日志不符合规范`
    )}\n\n${chalk.red(`  合法的提交日志格式如下(emoji 和 模块可选填)：\n\n`)}
    ${chalk.green(`✨ feat(模块): 添加了个很棒的功能`)}
    ${chalk.green(`🐛 fix(模块): 修复了一些 bug`)}
    ${chalk.green(`⚡ perf(模块): 优化了一些代码`)}
    ${chalk.green(`📝 docs(模块): 更新了一下文档`)}
    ${chalk.green(`💄 style(模块): 修改了一下样式`)}
    ${chalk.green(`🏰 chore(模块): 对脚手架做了些更改`)}
    ${chalk.green(`♻  refactor(模块): 重构了下代码`)}
    ${chalk.green(`✅ test(模块): 修改了一下测试`)}
    ${chalk.green(`⏪ revert(模块): 回滚到某个版本`)}
    ${chalk.red(`See .github/commit-convention.md for more details.\n`)}`
  )
  process.exit(1)
}
